#/usr/bin/env python

import os, pygame, math
from pyscape_settings import *
from pygame.locals import *



def dbg_print( txt ):
    if DEBUG:
        print txt

class right_click:

    boxes = []
    box_width = 160
    box_color = ( 0, 0, 0 )

    space_top = 5
    space_bot = 5
    space_left = 5

    lbl_txt = []
    lbl_en = []
    lbl_height = 15
    
    fnt = pygame.font.get_default_font()
    fnt_size = 12
    # active, highlight, deactive
    fnt_color = [ FOREGROUND_ACTIVE, FOREGROUND_DEACTIVE, FOREGROUND_HIGHLIGHT ]

    drp_shdw_size = 0
    drp_shdw_en = True
    drp_shdw_color = 0

    brd_size = 2
    brd_color = (20,20,20)
    brd_en = True

    mouse_pos = (0, 0)


    def __init__( self ): #, lbl_txt = [], lbl_en = [] ):
        print "init"
        #self.lbl_txt = lbl_txt
        #self.lbl_en = lbl_en


    def right_click( self, loc, screen, lbl_txt = "none", lbl_en = "none" ):
        if lbl_txt != "none":
            self.lbl_txt = lbl_txt
        if lbl_en != "none":
            self.lbl_en = lbl_en
        
        self.loc = loc
        self.show( screen )

        while True:
            pygame.time.wait(100)

            for event in pygame.event.get():
                if event.type == MOUSEMOTION:
                    self.mouse_pos = event.pos
                    self.show( screen )
                if event.type == MOUSEBUTTONUP:
                    #dbg_print( "Mouse up at: " + str(event.pos) )
                    i = self.get_lbl( )
                    #print "i is: " + str(i)
                    if i >= 0 and self.lbl_en[i] != DEACTIVE:
                        screen.blit(self.saved_screen, self.loc)
                        pygame.display.flip()
                        return self.lbl_txt[i]
                

    def show( self, screen, loc = "none" ):
        '''  Set the textbox at this location and return the selected text. '''
        if loc == "none":
            loc = self.loc
        else:
            self.loc = loc
       
        self.update_highlight()
        box_size = self.box_width, len( self.lbl_txt ) * self.lbl_height + self.space_top + self.space_bot
        box = self.make_box( box_size )
        
        for idx in range( len( self.lbl_txt ) ):
            fnt = pygame.font.Font( self.fnt, self.fnt_size )
            fnt_surface = fnt.render( self.lbl_txt[ idx ], 0, self.fnt_color[ self.lbl_en[ idx ] ] )
            box.blit( fnt_surface, ( self.space_left, self.lbl_height * idx + self.space_top ) )
        
        self.saved_screen = pygame.transform.chop( screen, box.get_rect() )

        screen.blit( box, loc )   
        pygame.display.flip()
    
    def update_highlight( self ):
        ''' Reset the highlight list base on curser position. '''
        i = self.get_lbl()

        if i < 0 or self.lbl_en[i] != ACTIVE:
            return
        
        for k in range( len( self.lbl_en ) ):
            if self.lbl_en[k] == HIGHLIGHT:
                self.lbl_en[ k ] = ACTIVE

        self.lbl_en[ i ] = HIGHLIGHT


    def get_lbl( self ):
        ''' Gets the label that the mouse is currently over.  Returns -1 if not found.'''
        #if self.mouse_pos[0] < self.loc[0] or self.mouse_pos[1] > ( self.loc[0] + self.box_width ):
        #    return -1
        #if self.mouse_pos[1] < self.loc[1] or self.mouse_pos[1] > self.loc[1] + self.space_top + self.space_bot + ( self.lbl_height * len(self.lbl_txt) ):
        #    return -1
        for i in range( len( self.lbl_txt ) ):
            #print "Looking:  "  + str(self.mouse_pos) + "  <  " + str( ( (i+1) * self.lbl_height ) + self.space_top + self.loc[1])
            if self.mouse_pos[1] <  ( (i+1) * self.lbl_height ) + self.space_top + self.loc[1]:
            
                return i
        return -1


    def make_box( self, size ):
        """ Returns a surface of the chosen size with the default settings. """
        for box in self.boxes:
            if box.get_size() == size:
                return box.copy()
        
        box = pygame.Surface( size )
        self.fill_gradient( surface=box, color=BACKGROUND, gradient=BACKGROUND_GRAD )

        br = size[0] - (self.brd_size + 1), size[1] - (self.brd_size + 1)
        bl = 0, size[1] - (self.brd_size + 1)
        tl = 0, 0
        tr = size[0] - (self.brd_size + 1), 0
        pygame.draw.line( box, BORDER, tl, tr, self.brd_size )
        pygame.draw.line( box, BORDER, tl, bl, self.brd_size )
        pygame.draw.line( box, BORDER, tr, br, self.brd_size )
        pygame.draw.line( box, BORDER, bl, br,self.brd_size )

        self.boxes.append( box.copy() )
        return box


    def fill_gradient(self, surface, color, gradient, vertical=True, forward=False ):
        """fill a surface with a gradient pattern
        Parameters:
        color -> starting color
        gradient -> final color
        rect -> area to fill; default is surface's rect
        vertical -> True=vertical; False=horizontal
        forward -> True=forward; False=reverse
        
        Pygame recipe: http://www.pygame.org/wiki/GradientCode
        """
        rect = surface.get_rect()
        x1,x2 = rect.left, rect.right
        y1,y2 = rect.top, rect.bottom
        if vertical: 
            h = y2-y1
        else:
            h = x2-x1
        if forward: 
            a, b = color, gradient
        else:
            b, a = color, gradient

        rate = ( float(b[0]-a[0])/h, float(b[1]-a[1])/h, float(b[2]-a[2])/h )
        fn_line = pygame.draw.line
        if vertical:
            for line in range( y1, y2 ):
                color = (
                    min(max(a[0]+(rate[0]*(line-y1)),0),255),
                    min(max(a[1]+(rate[1]*(line-y1)),0),255),
                    min(max(a[2]+(rate[2]*(line-y1)),0),255)
                )
                fn_line(surface, color, (x1,line), (x2,line))
        else:
            for col in range(x1,x2):
                color = (
                    min(max(a[0]+(rate[0]*(col-x1)),0),255),
                    min(max(a[1]+(rate[1]*(col-x1)),0),255),
                    min(max(a[2]+(rate[2]*(col-x1)),0),255)
                )
                fn_line(surface, color, (col,y1), (col,y2))
    
    
